package main

import (
	grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
	grpcrecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
	"log"
	global2 "longmen/server/config/global"
	"longmen/server/internal/rpc/hongbao/server"
	middleware2 "longmen/server/internal/rpc/middleware"
	"longmen/server/pkg/common/app"
	"longmen/server/pkg/common/app/etcd"
	appServer "longmen/server/pkg/common/app/server"
	"longmen/server/pkg/common/helper/const"
	pb "longmen/server/pkg/protobuf"
)

func main() {
	global2.AppName = "webapi_rpc_hongbao"
	global2.Initialize("../../../config/app.properties")
	limit := middleware2.NewAccountRequestLimit()
	grpcSrv := appServer.New(
		appServer.Network("tcp"),
		appServer.Address(":"+global2.Config.GetString("grpc.hong_bao_server_port")),
		appServer.UnaryServerInterceptor(
			grpcrecovery.UnaryServerInterceptor(appServer.RecoveryOption()...),
			middleware2.TokenValid,
			limit.RequestLimit,
			grpczap.UnaryServerInterceptor(global2.ZapLogger),
		),
	)
	grpcHongBaoServer := &server.Server{}
	//如果总是报红，在hongbao中的方法移到common中
	//go grpcHongBaoServer.BackHongBaoList()
	////消费红包队列把红包插入数据库中
	//go grpcHongBaoServer.ConsumerDiamondQueue()
	pb.RegisterHongBaoServerServer(grpcSrv, grpcHongBaoServer)
	app := app.NewApp(
		app.Version(global2.GetMicroserviceVersion()),
		app.Name(global2.GetMicroserviceName(_const.GRPC_HONG_BAO_SERVER_NAME_KEY)),
		app.Register(etcd.NewEtcdRegister(global2.GetEtcdCli())),
		app.Server(grpcSrv),
	)

	if err := app.Start(); err != nil {
		log.Fatalf("init account server is error: %+v \n", err)
	}
}
